package com.hsh.maoduo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hsh.maoduo.common.Result;
import com.hsh.maoduo.mapper.ShopGoodsMapper;
import com.hsh.maoduo.pojo.dto.ShopGoodsDTO;
import com.hsh.maoduo.pojo.entity.ShopGoods;
import com.hsh.maoduo.pojo.vo.ShopGoodsVO;
import com.hsh.maoduo.service.ShopGoodsService;
import com.hsh.maoduo.util.MyBeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author USER
 * @description 针对表【shop_goods】的数据库操作Service实现
 * @createDate 2024-03-21 13:44:35
 */
@Service
public class ShopGoodsServiceImpl extends ServiceImpl<ShopGoodsMapper, ShopGoods>
        implements ShopGoodsService {


    @Resource
    private ShopGoodsMapper shopGoodsMapper;


    /**
     * 列表查询
     *
     * @param shopGoodsVO
     * @return
     */
    public List<ShopGoodsDTO> getList(ShopGoodsVO shopGoodsVO) {
        List<ShopGoodsDTO> shopGoodsDTOList = shopGoodsMapper.getList(shopGoodsVO);
        shopGoodsDTOList.stream().forEach(v -> v.setGoodsInfo(v.getGoodsName() + "*" + v.getNum()));
        return shopGoodsDTOList;
    }

    /**
     * 批量添加
     *
     * @param shopGoodsVOList
     * @return
     */
    @Override
    public Result<Integer> addList(List<ShopGoodsVO> shopGoodsVOList) {
        int insert = 0;
        for (ShopGoodsVO shopGoodsVO : shopGoodsVOList) {
            ShopGoods shopGoods = MyBeanUtils.copy(shopGoodsVO, ShopGoods.class);
            insert = shopGoodsMapper.insert(shopGoods);
        }
        return Result.create(insert);
    }

    /**
     * 修改
     *
     * @param shopGoodsVO
     * @return
     */
    @Override
    public Result update(ShopGoodsVO shopGoodsVO) {
        ShopGoods shopGoods = MyBeanUtils.copy(shopGoodsVO, ShopGoods.class);
        return Result.create(shopGoodsMapper.updateById(shopGoods));
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public Result delete(int id) {
        return Result.create(shopGoodsMapper.deleteById(id));
    }


    /**
     * 根据店铺商品id 获取 商品名称
     *
     * @param shopGoodsVO
     * @return map （店铺商品id，商品名称）
     */
    @Override
    public List<ShopGoodsDTO> selectGoodsNameList(ShopGoodsVO shopGoodsVO) {
        List<ShopGoodsDTO> shopGoodsDTOList = shopGoodsMapper.selectGoodsNameList(shopGoodsVO);
        shopGoodsDTOList.stream().forEach(ShopGoodsDTO::init);
        return shopGoodsDTOList;
    }
//    public Map<String, String> selectGoodsNameList(ShopGoodsVO shopGoodsVO) {
//        List<ShopGoodsDTO> shopGoodsDTOS = shopGoodsMapper.selectGoodsNameList(shopGoodsVO);
//        Map<String, String> map = new HashMap<>();
//        for (ShopGoodsDTO shopGoodsDTO : shopGoodsDTOS) {
//            String goodsName = shopGoodsDTO.getGoodsName();
//            if (Strings.isNotBlank(shopGoodsDTO.getSimpleGoodsName())) {
//                goodsName = shopGoodsDTO.getSimpleGoodsName();
//            }
//            map.put(shopGoodsDTO.getShopGoodsId(), goodsName + "*" + shopGoodsDTO.getNum());
//        }
//        return map;
//    }
}




